import $state from '../state/index.js';
const db = uniCloud.database();
export default {
  get() {
    return $state.friend.dataList
  },
  async loadMore({
    friend_uid
  } = {}) {
    let whereString = '"user_id" == $cloudEnv_uid'
    if (friend_uid) {
      whereString += `&& "friend_uid" == "${friend_uid}"`
     // console.log('whereString',whereString);
    }
	 console.log('进入loadMore');
    let res = await db.collection(
        db.collection('uni-im-friend').where(whereString).field('friend_uid,mark,class_name')
        .getTemp(),
        db.collection('uni-id-users').field('_id,nickname,avatar_file').getTemp()
      )
      .limit(500)
      .get()
	  console.log('data123',res);
    let data = res.result.data
    
    data.forEach((item, index) => {
      data[index] = item.friend_uid[0]
      let uid = data[index]._id
      if (!$state.users[uid]) {
        $state.users[uid] = item.friend_uid[0]
      }
    })
    $state.friend.hasMore = data.length == 500
    $state.friend.dataList.push(...data)
	// 去重  __WJF
	// 使用 Set 来记录已出现过的 _id
	const seen = new Set();
	$state.friend.dataList = $state.friend.dataList.filter(item => {
	    const duplicate = seen.has(item._id);
	    seen.add(item._id);
	    return !duplicate;
	});
	// console.log($state.friend.dataList);
  },
  remove(friend_uid) {
    let friendList = $state.friend.dataList
    let index = friendList.findIndex(i => i._id == friend_uid)
    friendList.splice(index, 1)
  }
}
